package com.example.base.websocket;

import com.example.base.websocket.interceptor.AppWebSocketInterceptor;
import com.example.base.websocket.interceptor.WebSocketInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private SystemWebSocketHandler systemWebSocketHandler;

    @Autowired
    private WebSocketInterceptor webSocketInterceptor;

    @Autowired
    private AppWebSocketHandler appWebSocketHandler;

    @Autowired
    private AppWebSocketInterceptor appWebSocketInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(systemWebSocketHandler, "ws")
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");

        registry
                .addHandler(appWebSocketHandler,"ws-app/**")
                .addInterceptors(appWebSocketInterceptor)
                .setAllowedOrigins("*");
    }
}
